package com.bestcem.xm.txtanls.grpc.service;


import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.uitls.GRpcUtil;
import com.bestcem.xm.txtanls.grpc.convert.WorktableConvert;
import com.bestcem.xm.txtanls.grpc.service.param.worktable.ListWorktableParam;
import com.bestcem.xm.txtanls.grpc.v1.services.*;
import com.bestcem.xm.txtanls.service.WorktableService;
import com.bestcem.xm.txtanls.service.dto.worktable.WorktableFromBIDTO;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.Set;

/**
 * @author liheng
 * @version v1.0
 * @date 2021/6/21 19:05
 */
@Slf4j
@GrpcService
public class WorktableGrpcService extends WorktableServiceGrpc.WorktableServiceImplBase {

    @Resource
    private WorktableService workTableService;

    /**
     * 查询单个数据源信息
     *
     * @param request
     * @param responseObserver
     */
    @Override
    public void getWorktable(GetWorktableRequest request, StreamObserver<GetWorktableResponse> responseObserver) {
        GetWorktableResponse.Builder builder = GetWorktableResponse.newBuilder();

        String worktableId = request.getWorktableId();
        if (StringUtils.isBlank(worktableId) || !ObjectId.isValid(worktableId)) {
            builder.setStatus(RpcStatusResult.error("worktableId错误"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        ServiceResult<WorktableFromBIDTO> result = workTableService.getWorktable(worktableId);
        if (result.isSuccess()) {
            builder.setWorktable(WorktableConvert.convertToGrpc(result.getData()));
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
        return;
    }

    @Override
    public void listWorktable(ListWorktableRequest request, StreamObserver<ListWorktableResponse> responseObserver) {
        ListWorktableResponse.Builder builder = ListWorktableResponse.newBuilder();
        ListWorktableParam listWorkTableParam = new ListWorktableParam(request.getOrgId(), request.getUserId());
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(listWorkTableParam);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        ServiceResult<List<WorktableFromBIDTO>> result = workTableService.listWorktable(listWorkTableParam);
        if (result.isSuccess()) {
            builder.addAllWorktables(WorktableConvert.convertToGrpc(result.getData()));
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
        return;
    }
}
